package mysql

import (
	"gorm.io/gorm"
	"novel-start/novelrpc/novelrpc"
)

type BookShelf struct {
	gorm.Model
	UserId  int64 `gorm:"type:int(11);not null"`
	NovelId int64 `gorm:"type:int(11);not null"`
}

func (s *BookShelf) BookShelfAdd(db *gorm.DB) error {
	return db.Debug().Create(s).Error
}

func (s *BookShelf) BookShelfDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Delete(s).Error
}

func BookShelfList(db *gorm.DB, in *novelrpc.BookShelfListRequest, list []*novelrpc.BookShelfList) []*novelrpc.BookShelfList {
	db.Debug().Model(&BookShelf{}).
		Select("book_shelves.id as book_shelf_id,novels.*,nick_name,head_image").
		Joins("left join users on users.id = book_shelves.user_id").
		Joins("left join novels on novels.id = book_shelves.novel_id").
		Where("book_shelves.id = ?", in.UserId).
		Find(&list)
	return list
}
